package org.dtrd.modules.food.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.food.entity.bean.FoodCategoryDetail;
import org.dtrd.modules.food.entity.po.DtrdExtEmsYwFoodCategory;
import org.dtrd.modules.food.entity.request.FoodCategoryRequest;
import org.dtrd.modules.food.mapper.DtrdExtEmsYwFoodCategoryMapper;
import org.dtrd.modules.food.service.IDtrdExtEmsYwFoodCategoryService;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 食材分类 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-08
 */
@DS("multi-datasource1")
@Service
public class DtrdExtEmsYwFoodCategoryServiceImpl extends ServiceImpl<DtrdExtEmsYwFoodCategoryMapper, DtrdExtEmsYwFoodCategory> implements IDtrdExtEmsYwFoodCategoryService {

    private DtrdExtEmsYwFoodCategoryMapper dtrdExtEmsYwFoodCategoryMapper;

    @Override
    public List<FoodCategoryDetail> getFoodCategoryList(FoodCategoryRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwFoodCategory> queryWrapper = baseQueryWrapper();
        String foodCategoryName = request.getFoodCategoryName();
        if (foodCategoryName != null) {
            queryWrapper.like(DtrdExtEmsYwFoodCategory::getCategoryName, StringUtil.fuzzySearchWrapper(foodCategoryName));
        }
        List<DtrdExtEmsYwFoodCategory> poList = list(queryWrapper);
        List<FoodCategoryDetail> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream()
                .map(po -> new FoodCategoryDetail().parseFromPo(po))
                .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public FoodCategoryDetail getFoodCategory(Integer dataId) {
        DtrdExtEmsYwFoodCategory dtrdExtEmsYwFoodCategory = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwFoodCategory)
                        .map(po -> new FoodCategoryDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateFoodCategory(FoodCategoryDetail dto) {
        DtrdExtEmsYwFoodCategory dtrdExtEmsYwFoodCategory = new DtrdExtEmsYwFoodCategory();
        if(dto.getDataId() == null){
            dtrdExtEmsYwFoodCategory = dtrdExtEmsYwFoodCategory.init();
        }
        dtrdExtEmsYwFoodCategory.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwFoodCategory);
    }

    @Override
    public boolean removeFoodCategory(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdExtEmsYwFoodCategory> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwFoodCategory.class)
                        .eq(DtrdExtEmsYwFoodCategory::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwFoodCategory> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwFoodCategory.class)
                        .eq(DtrdExtEmsYwFoodCategory::getIsDel, 0);
    }
}
